//
//  CarouselScrollView.m
//  CapriceMusic
//
//  Created by 蒋恩浩 on 16/1/12.
//  Copyright © 2016年 MYX. All rights reserved.
//

#import "CarouselScrollView.h"

@interface CarouselScrollView ()<UIScrollViewDelegate>

@end

// 一个单元宽度
static CGFloat onceWidth = 0;
// 数组个数
static NSInteger count = 0;

@implementation CarouselScrollView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.frame = frame;
        [self setBounces:NO];
        self.delegate = self;
        self.scrollEnabled = NO;
        self.showsHorizontalScrollIndicator = NO;
    }
    return self;
}

- (void)createScrollViewWithArray:(NSArray *)array withTitleColor:(UIColor *)color {
    
    onceWidth = self.width / (CGFloat)array.count;
    count = array.count;
    for (NSInteger j = 0; j < 3; j++) {
        
        NSInteger num = 0;
        for (NSInteger i = j * count; i < j * count + count; i++) {
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(onceWidth * i, 0, onceWidth, self.height);
            [button setTitle:array[num++] forState:UIControlStateNormal];
            [button setFont:[UIFont systemFontOfSize:18]];
            [button setTitleColor:color forState:UIControlStateNormal];
            button.tag = 100 + i;
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
            if (i == 7) {
                
                [button setTitleColor:[UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0] forState:UIControlStateNormal];
            } else {
                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }
        }
    }
    self.contentSize = CGSizeMake(onceWidth * count * 3, 0);
    [self setContentOffset:CGPointMake(onceWidth * count, 0)];
}

- (void)buttonAction:(UIButton *)sender {
    
    for (int i = 5; i < count * 2 && i > count - 1; i++) {

        UIButton *button = (UIButton *)[self viewWithTag:100 + i];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    NSInteger temp = sender.tag - 100;
    [self setContentOffset:CGPointMake(onceWidth * (temp - 2), 0)];
    if (temp >= 10) {
        
        [self setContentOffset:CGPointMake(onceWidth * (temp - count - 2), 0)];
        UIButton *button = (UIButton *)[self viewWithTag:100 + temp - count];
        [button setTitleColor:[UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        temp -= count;
    } else if (temp <= 4) {
        
        [self setContentOffset:CGPointMake(onceWidth * (temp + count - 2), 0)];
        UIButton *button = (UIButton *)[self viewWithTag:100 + temp + count];
        [button setTitleColor:[UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        temp += count;
    } else {
        [sender setTitleColor:[UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    }
    self.changeAreaBlock(temp);
}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    
//    // 滑到最后一个偏移到第一个
//    if (scrollView.contentOffset.x == onceWidth * 10) {
//        
//        [scrollView setContentOffset:CGPointMake(onceWidth * 5, 0)];
//    }
//    // 滑到第一个偏移到最后一个
//    if (scrollView.contentOffset.x == onceWidth * 0) {
//        
//        [scrollView setContentOffset:CGPointMake(onceWidth * 5, 0)];
//    }
//}

@end












